package com.gzhryc.shared_device.oem.code.device;

import com.gzhryc.common.StringTools;
import com.gzhryc.shared_device.base.IDeviceEventListener;
import com.gzhryc.shared_device.cdb.code.services.BatteryCabinetService;
import com.gzhryc.shared_device.cdcz.code.services.ChargeSocketService;
import com.gzhryc.shared_device.common.device.dao.db.DeviceTypeFunc;
import com.gzhryc.shared_device.common.device.services.DeviceTypeFuncService;

import java.util.List;

public class DeviceEventListener implements IDeviceEventListener {

    @Override
    public boolean updateDeviceTypeName(String deviceTypeId, String deviceTypeName) {
        if(StringTools.isNotBlank(deviceTypeId) && StringTools.isNotBlank(deviceTypeName)){
            List<DeviceTypeFunc> deviceTypeFuncList = DeviceTypeFuncService.self().findByTypeId(deviceTypeId);
            if(deviceTypeFuncList != null && deviceTypeFuncList.size() > 0){
                boolean flag = true;
                for(DeviceTypeFunc deviceTypeFunc : deviceTypeFuncList){
                    if(EDeviceFunCode.CDCZ.index().equals(deviceTypeFunc.getCode())){
                        flag = flag && ChargeSocketService.self().updateDeviceTypeName(deviceTypeId,deviceTypeName);
                    }else if(EDeviceFunCode.CDB.index().equals(deviceTypeFunc.getCode())){
                        flag = flag && BatteryCabinetService.self().updateDeviceTypeName(deviceTypeId,deviceTypeName);
                    }
                    if(!flag){
                        return false;
                    }
                }
                return flag;
            }else{
                return true;
            }
        }
        return false;
    }
}
